package com.miyake.client.datamanage;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.client.DataServiceAsync;
import com.miyake.client.tools.PlotListDialog;
import com.miyake.client.tools.ShowDialog;
import com.miyake.client.tools.SmallButton;
import com.miyake.client.tools.WaitPopup;

public class PlotPage extends Page {
	private static int XNUMBER = 501;
	private DynamicChart dchart;
	public PlotPage(NodeProperty node, VerticalPanel panel) {
		super(node, panel);
	}
	
	@Override
	protected Widget getContent(final NodeProperty node, final DataServiceAsync service) {
		final VerticalPanel panel = new VerticalPanel();

//		createCombo(panel);
		//createChart(panel);
//		createToobar(node, panel);
		//getPlot(node.getKey());
		
		dchart = new DynamicChart() {
			@Override
			void onScaleChange(double x, double y, double spanx, double spany) {
				PlotSetting scale = new PlotSetting();
				
				scale.setXmax(x + spanx / 2.0);
				scale.setXmin(x - spanx / 2.0);
				scale.setYmax(y + spany / 2.0);
				scale.setYmin(y - spany / 2.0);
				service.updatePlot(getNode().getKey(), scale, new AsyncCallback<Void>(){
					@Override
					public void onFailure(Throwable caught) {
					}

					@Override
					public void onSuccess(Void result) {
						updateChart();
					}
				});
			}
		};
		
		panel.add(dchart);
		getPlot(node.getKey(), panel);
		return panel;
	}
	
	private void getPlot(final String key, VerticalPanel panel) {
		final VerticalPanel vpanel = new VerticalPanel();
		vpanel.add(new Label("Overlayed Charts"));
		panel.add(vpanel);
		
		getService().getPlot(key, XNUMBER, new AsyncCallback<PlotData>(){
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(PlotData result) {
				dchart.addPlot(result, key);
				for (String s: result.getOverlays()) {
					createOverlayItem(vpanel, s);
					getPlot(s);
				}
			}	
		});
	}

	protected void getOverlays(List<String> overlays) {
		for (String key : overlays) {
			getPlot(key);
		}
	}

	protected void getPlot(final String key) {
		WaitPopup.showPopup();
		getService().getPlot(key, XNUMBER, new AsyncCallback<PlotData>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(PlotData result) {
				//drawOverlay(result);
				dchart.addPlot(result, key);
				WaitPopup.hidePopup();
			}
			
		});
	}

	protected void createToobar(final NodeProperty node, VerticalPanel panel) {
		HorizontalPanel toolBar = new HorizontalPanel();
		toolBar.setWidth("100%");
		panel.add(toolBar);
		
		HorizontalPanel zoomPanel = new HorizontalPanel();
		zoomPanel.setBorderWidth(1);
		toolBar.add(zoomPanel);
		zoomPanel.add(new SmallButton("Zoom in", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				//dchart.zoomIn();
			}
		}));
		
		zoomPanel.add(new SmallButton("Zoom Out", new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				//dchart.zoomOut();
			} 
		}));
		
		zoomPanel.add(new SmallButton("Default", new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				PlotSetting scale = new PlotSetting();
				scale.setDefaultScale(true);
				getService().updatePlot(getNode().getKey(), scale, new AsyncCallback<Void>() {
					@Override
					public void onFailure(Throwable caught) {
					}

					@Override
					public void onSuccess(Void result) {
						updateChart();
					}
				});
			} 
		}));	
		
		HorizontalPanel markerPanel = new HorizontalPanel();
		markerPanel.setBorderWidth(1);
		toolBar.add(markerPanel);
		Button marker = new SmallButton("Marker");
		marker.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				dchart.addMarker();
			}
		});
		markerPanel.add(marker);
/*		
		SmallButton update = new SmallButton("Update", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				dchart.update();
			}
		});
		toolBar.add(update);
*/		
		HorizontalPanel overlayPanel = new HorizontalPanel();
		overlayPanel.setBorderWidth(1);
		overlayPanel.add(new Label("Overlay"));
		SmallButton select = new SmallButton("Select Chart", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				showChartListDialog();
			}
		});
		overlayPanel.add(select);
		toolBar.add(overlayPanel);
		
		
		
		HorizontalPanel chartTypePanel = new HorizontalPanel();
		chartTypePanel.setBorderWidth(1);
		ListBox chartTypeCombo = new ListBox();
		chartTypeCombo.addItem("Line");
		chartTypeCombo.addItem("Dot");
		chartTypePanel.add(new Label("Chart Type"));
		chartTypePanel.add(chartTypeCombo);
		
		toolBar.add(chartTypePanel);
	}

	private void createCombo(VerticalPanel panel) {
		HorizontalPanel hpanel = new HorizontalPanel();
		panel.add(hpanel);
		hpanel.add(new Label("Overlay"));
		final ListBox list = new ListBox();
		hpanel.add(list);
		hpanel.add(new SmallButton("Add", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				addOverlay(list.getValue(list.getSelectedIndex()));
			}
		}));
		
		hpanel.add(new SmallButton("Delete", new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				deleteOverlay(list.getValue(list.getSelectedIndex()));
			}
			
		}));
		
		hpanel.add(new SmallButton("Select Chart", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				showChartListDialog();
			}
		}));
		
		final VerticalPanel vpanel = new VerticalPanel();
		panel.add(vpanel);
		
		getService().getSpecifiedSubNodes(getNode().getParentKey(), NodeProperty.PLOT, new AsyncCallback<List<NodeProperty>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<NodeProperty> result) {
				for (NodeProperty p : result) {
					if (!p.getKey().equals(getNode().getKey())) {
						list.addItem(p.getName(), p.getKey());
						final CheckBox check = new CheckBox(p.getName());
						check.setName(p.getKey());
						check.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
							@Override
							public void onValueChange(
									ValueChangeEvent<Boolean> event) {
								if (check.getValue()) {
									addOverlay(check.getName());
								}
								else {
									deleteOverlay(check.getName());
								}
							}
						});
						vpanel.add(check);
					}
				}
			}
			
		});
	}

	protected void showChartListDialog() {
		final PlotListDialog dlg = new PlotListDialog(getNode().getParentKey());
		new ShowDialog() {
			@Override
			protected void onOK(String text) {
				addOverlay(dlg.getSelectedKey());
			}

			@Override
			protected DialogBox getDialogBox() {
				return dlg;
			}
			
		};
	}

	protected void deleteOverlay(final String overlayNodeKey) {
		getService().deletePlotOverLay(getNode().getKey(), overlayNodeKey, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Void result) {
				dchart.remove(overlayNodeKey);
			}	
		});
	}

	protected void addOverlay(final String overlayNodeKey) {
		getService().addPlotOverlay(getNode().getKey(), overlayNodeKey, new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(String result) {
				getPlot(overlayNodeKey);
			}
		});
	}

	protected void createOverlayItem(final VerticalPanel vpanel, String s) {
		final HorizontalPanel p = new HorizontalPanel();
		vpanel.add(p);
		getService().getNode(s, new AsyncCallback<NodeProperty>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(final NodeProperty result) {
				final Label label = new Label(result.getName());
				final SmallButton button = new SmallButton("Delete", new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						deleteOverlay(result.getKey());
						label.removeFromParent();
						((Button)event.getSource()).removeFromParent();
					}
				});
				p.add(label);
				p.add(button);
			}
			
		});
	}

	protected void updateChart() {
		dchart.clear();
		getPlot(getNode().getKey());
	}


}
